package com.annchain.og_browser.util;

import org.springframework.format.annotation.DateTimeFormat;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtil {
    static DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    //系统时间

    public static String DataDiff(Date time) {
        Date date = new Date();
        long diff = 0;
        diff = date.getTime() - time.getTime();
        //计算天数
        long days = diff / (1000 * 60 * 60 * 24);
        //计算小时
        long hours = (diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        //计算分钟
        long minutes = (diff % (1000 * 60 * 60)) / (1000 * 60);
        //计算秒
        long seconds = (diff % (1000 * 60)) / 1000;

        if (days != 0) {
            return days + "天"+ hours + "小时前";
        }
        if (hours != 0) {
            return hours + "小时" + minutes + "分钟前";
        }
        if (minutes != 0) {
            return minutes + "分钟" + seconds + "秒前";
        }
        if (seconds != 0) {
            return seconds + "秒前";
        }
        return "1秒前";
    }

    public static String TimeStampToString (Date time) {
        String str = "";
        str = dateFormat.format(time);
        return str;
    }

    public static Long OutputTimeDiff(Date preTime, Date nowTime) {
        Long diff = null;
        if (nowTime != null && preTime != null)

            diff = nowTime.getTime() - preTime.getTime();
        return diff;
    }

}
